/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * disenio.java
 *
 * Created on 13/08/2012, 06:48:14 AM
 */
package seriedetaylor;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author alito
 */
public class disenio extends javax.swing.JFrame {
    //definimos objeto para uso de la clase Serie de Taylor    
    private SerieDeTaylor serie;
    DefaultTableModel modelo;
    TableColumn ancho; 
    /** Creates new form disenio */
    public disenio() {
        initComponents();
        serie=new SerieDeTaylor();
        modelo=(DefaultTableModel)tabla.getModel();       
        
    }

    public void Mostrar(){        
        modelo.setColumnCount(4);
        ancho=tabla.getColumnModel().getColumn(0);        
        ancho.setPreferredWidth(10);       
        ancho=tabla.getColumnModel().getColumn(1);        
        ancho.setPreferredWidth(180);
        ancho=tabla.getColumnModel().getColumn(2);        
        ancho.setPreferredWidth(180);       
        ancho=tabla.getColumnModel().getColumn(3);        
        ancho.setPreferredWidth(180);
        tabla.setRowHeight(30);
        for (int i = 0; i < serie.getInteracciones(); i++) {
            modelo.setNumRows(i+1);
            modelo.setValueAt(i, i, 0);
            modelo.setValueAt(serie.getVector(i), i, 1);
            modelo.setValueAt(serie.getVectorAcum(i), i, 2);
            modelo.setValueAt(serie.getVectorError(i), i, 3);
            
        }
    }
    
    private  boolean validarTrigonometrica(){
        double numero=Double.parseDouble(x.getText());
        if(numero>=-2*Math.PI && numero<=2*Math.PI)
            return true;
          else
            return false;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel3 = new javax.swing.JLabel();
        x = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        R2 = new javax.swing.JRadioButton();
        R3 = new javax.swing.JRadioButton();
        R1 = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        interacciones = new javax.swing.JTextField();
        res2 = new javax.swing.JLabel();
        res1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setFont(new java.awt.Font("Ubuntu", 1, 24));
        jLabel3.setText("Serie De Taylor");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, -1, -1));

        x.setFont(new java.awt.Font("Tahoma", 1, 14));
        getContentPane().add(x, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 80, 80, 30));

        jButton1.setText("Aproximar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 80, -1, -1));

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 80, -1, -1));

        tabla.setFont(new java.awt.Font("Ubuntu", 0, 16)); // NOI18N
        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Interacion", "F(x)'", "Suma Acumulada", "Error"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tabla);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 130, 670, 220));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(2, 100, 233), 2, true), "Funciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 14), new java.awt.Color(20, 76, 244))); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        buttonGroup1.add(R2);
        R2.setText("cos(x)_______ [-2Pi,2PI]");
        R2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                R2ActionPerformed(evt);
            }
        });
        jPanel1.add(R2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        buttonGroup1.add(R3);
        R3.setText("euler(x)_____Para todo X");
        R3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                R3ActionPerformed(evt);
            }
        });
        jPanel1.add(R3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 110, -1, -1));

        buttonGroup1.add(R1);
        R1.setSelected(true);
        R1.setText("sen(x)_______ [-2Pi,2PI]");
        R1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                R1ActionPerformed(evt);
            }
        });
        jPanel1.add(R1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, -1, 25));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(690, 120, 230, 230));

        jLabel1.setText("X:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 60, -1, -1));

        jLabel2.setText("N° INteracciones");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, -1, -1));

        interacciones.setFont(new java.awt.Font("Tahoma", 1, 14));
        interacciones.setText("20");
        getContentPane().add(interacciones, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, 80, 30));

        res2.setFont(new java.awt.Font("Ubuntu", 1, 24));
        res2.setText("0");
        getContentPane().add(res2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 390, -1, -1));

        res1.setFont(new java.awt.Font("Ubuntu", 1, 24));
        res1.setText("0");
        getContentPane().add(res1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 360, -1, -1));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-937)/2, (screenSize.height-477)/2, 937, 477);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      //validamo caja de texto
        if(!x.getText().equals("") && !interacciones.getText().equals("")){
       //obtenemos el valor para x        
       double total=0;
       serie.setX(Float.parseFloat(x.getText()));
       serie.setInteracciones(Integer.parseInt(interacciones.getText()));
       if(R1.isSelected()){
        if(validarTrigonometrica()){
        total=serie.seno();   
        Mostrar();
        modelo.setRowCount(serie.getInteracciones()+1);
        modelo.setValueAt("Aproximacion: ", serie.getInteracciones(), 0);
        modelo.setValueAt(total, serie.getInteracciones(), 1);
        res2.setText("Valor Real de Sen("+x.getText()+") = "+String.valueOf(Math.sin(serie.getX())));
        res1.setText("Valor Aproximado de Sen("+x.getText()+") = "+String.valueOf(total));
        
        }
        else
            JOptionPane.showMessageDialog(this, "EL Valor de X no esta dentro del rango [-2Pi,2PI]");
       }
       if(R2.isSelected()){         
        if(validarTrigonometrica()){
        total=serie.coseno();   
        Mostrar();
        modelo.setRowCount(serie.getInteracciones()+1);
        modelo.setValueAt("Aproximacion: ", serie.getInteracciones(), 0);
        modelo.setValueAt(total, serie.getInteracciones(), 1);  
        res2.setText("Valor Real de Cos("+x.getText()+") = "+String.valueOf(Math.cos(serie.getX())));
        res1.setText("Valor Aproximado de Cos("+x.getText()+") = "+String.valueOf(total));
        }
        else
            JOptionPane.showMessageDialog(this, "EL Valor de X no esta dentro del rango [-2Pi,2PI]");
       }
       if(R3.isSelected()){
        total=serie.euler();   
        Mostrar();
        modelo.setRowCount(serie.getInteracciones()+1);
        modelo.setValueAt("Aproximacion: ", serie.getInteracciones(), 0);
        modelo.setValueAt(total, serie.getInteracciones(), 1);  
        res2.setText("Valor Real de Euler("+x.getText()+") = "+String.valueOf(Math.exp(serie.getX())));
        res1.setText("Valor Aproximado de Euler("+x.getText()+") = "+String.valueOf(total));
       }
      }
        else{
            JOptionPane.showMessageDialog(this, "El Valor de X o De interacciones Esta Vacio");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
        System.exit(0);               
}//GEN-LAST:event_jButton2ActionPerformed

    private void R2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_R2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_R2ActionPerformed

    private void R3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_R3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_R3ActionPerformed

    private void R1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_R1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_R1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new disenio().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton R1;
    private javax.swing.JRadioButton R2;
    private javax.swing.JRadioButton R3;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField interacciones;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel res1;
    private javax.swing.JLabel res2;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField x;
    // End of variables declaration//GEN-END:variables
}
